package common;

public class Util {
    /**
     * 创建链表，默认10个大小
     *
     * @return
     */
    public static ListNode createListNode() {
        return createListNode(10);
    }

    /**
     * 创建指定Size的链表
     *
     * @param n 大小
     * @return
     */
    public static ListNode createListNode(int n) {
        ListNode head = new ListNode();
        head.val = 1;
        ListNode before = head;
        for (int i = 2; i < n + 1; i++) {
            ListNode ListNode = new ListNode();
            ListNode.val = i;
            before.next = ListNode;
            before = before.next;
        }
        return head;
    }

    public static void print2Array(int[][] arr) {
        for (int i = 0; i < arr.length; i++) {
            int[] a = arr[i];
            for (int j = 0; j < a.length; j++) {
                System.out.print(arr[i][j] + getBlank(arr[i][j]));
            }
            System.out.println();
        }
    }

    private static String getBlank(int value) {
        if (value > 1000) {
            return "";
        }
        if (value > 100) {
            return " ";
        }
        if (value >= 10) {
            return "  ";
        }
        return "   ";
    }

    /**
     * 格式化输出ListNode信息
     *
     * @param head 头节点
     * @return
     */
    public static String formatListNode(ListNode head) {
        if (head == null)
            return null;
        StringBuilder result = new StringBuilder();
        while (head != null) {
            result.append(head.val);
            if (head.next != null && head.next.prev != null) {
                result.append(" <-> ");
            }
            if (head.next != null && head.next.prev == null) {
                result.append(" -> ");
            }
            head = head.next;
        }
        System.out.println(result.toString());
        return result.toString();
    }

}